/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.quickstart.adapter;

import com.wordplat.quickstart.ResourceTable;
import com.wordplat.quickstart.ability.Disable_Left_And_Right_Refresh_Ability;
import com.wordplat.quickstart.ability.Enable_Left_And_Right_Refresh_Ability;
import com.wordplat.quickstart.ability.MACD_RSI_KDJ_Show_Together_Ability;
import com.wordplat.quickstart.ability.Simple_TimeLine_Example_Ability;
import com.wordplat.quickstart.ability.With_Fragment_And_TabLayout_Switcher_Example_Ability;
import com.wordplat.quickstart.ability.With_Pull_To_Refresh_Example_Ability;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by afon on 2017/2/10.
 *
 * @since 2021-05-08
 */
public class TextAdapter extends BaseItemProvider {
    private static final List<String> textList = new ArrayList<>();
    private static final int NUM_0 = 0;
    private static final int NUM_1 = 1;
    private static final int NUM_2 = 2;
    private static final int NUM_3 = 3;
    private static final int NUM_4 = 4;
    private static final int NUM_5 = 5;

    private final LayoutScatter inflater;


    static {
        textList.add("左滑右滑加载(Enable left and right refresh)");
        textList.add("禁用左滑右滑加载");
        textList.add("多个指标共同显示、联动(MACD, RSI, KDJ)");
        textList.add("在页面中使用");
        textList.add("带有下拉刷新的需求中使用");
        /**
         * textList.add("动态改变颜色、尺寸配置"); // 还没有准备好
         * textList.add("在 RecyclerView 列表中使用"); // 还没有准备好
         */
        textList.add("简单分时图");
    }

    private final AbilitySlice mAbilitySlice;

    /**
     * TextAdapter
     *
     * @param slice
     */
    public TextAdapter(AbilitySlice slice) {
        mAbilitySlice = slice;
        inflater = LayoutScatter.getInstance(slice);
    }

    @Override
    public int getCount() {
        return textList.size();
    }

    @Override
    public Object getItem(int i) {
        return textList.get(i);
    }

    /**
     * getItemId
     *
     * @param itemId
     * @return long number
     */
    @Override
    public long getItemId(int itemId) {
        return itemId;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        component = inflater.parse(ResourceTable.Layout_item_text, componentContainer, false);
        Component view = component.findComponentById(ResourceTable.Id_view);
        Text text = (Text) component.findComponentById(ResourceTable.Id_text);
        text.setText(textList.get(i));
        if (i == getCount() - 1) {
            view.setVisibility(Component.HIDE);
        }
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                switch (i) {
                    case NUM_0:
                        nextPage(Enable_Left_And_Right_Refresh_Ability.class);
                        break;

                    case NUM_1:
                        nextPage(Disable_Left_And_Right_Refresh_Ability.class);
                        break;

                    case NUM_2:
                        nextPage(MACD_RSI_KDJ_Show_Together_Ability.class);
                        break;

                    case NUM_3:
                        nextPage(With_Fragment_And_TabLayout_Switcher_Example_Ability.class);
                        break;
                    case NUM_4:
                        nextPage(With_Pull_To_Refresh_Example_Ability.class);
                        break;
                    case NUM_5:
                        nextPage(Simple_TimeLine_Example_Ability.class);
                        break;
                    default:
                        break;
                }
            }
        });
        return component;
    }

    /**
     * nextPage
     *
     * @param pageName
     */
    public void nextPage(Class pageName) {
        Intent secondIntent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(mAbilitySlice.getBundleName())
                .withAbilityName(pageName.getName())
                .build();
        secondIntent.setOperation(operation);
        mAbilitySlice.startAbility(secondIntent);
    }
}
